<template>
  <div style="text-align: center">
    <!--三级分类组件-->
    <sph-typeNav></sph-typeNav>
    <!--商品区域-->
    <div class="main">
      <div class="py-container">
        <!--bread-->
        <div class="bread">
          <ul class="fl sui-breadcrumb">
            <li>
              <a href="#">全部结果</a>
            </li>
          </ul>
          <ul class="fl sui-tag">
            <!--分类-->
            <li class="with-x" v-if="options.categoryName">
              {{ level }}级分类:{{ options.categoryName }}
              <i class="icon-close" @click="removeCategory"></i>
            </li>
            <!--关键字-->
            <li class="with-x" v-if="options.keyword">
              关键字:{{ options.keyword }}
              <i class="icon-close" @click="removeKeyWord"></i>
            </li>
            <!--品牌-->
            <li class="with-x" v-if="options.trademark">
              品牌:{{ options.trademark.split(":")[1] }}
              <i class="icon-close" @click="removeTrademark"></i>
            </li>
            <!--销售属性-->
            <li
              class="with-x"
              v-for="(prop, index) in options.props"
              :key="index"
            >
              {{ prop.split(":")[1] }}
              <i class="icon-close" @click="removeProp(index)"></i>
            </li>
          </ul>
        </div>

        <!--selector-->
        <sph-searchSelector></sph-searchSelector>

        <!--details-->
        <div class="details clearfix">
          <!--排序相关按钮-->
          <div class="sui-navbar">
            <!--排序按钮组-->
            <sph-sortBtns
              :btns="sortBtns"
              :order="options.order"
              @btnChange="btnChangeFn"
            ></sph-sortBtns>
          </div>
          <!--商品列表-->
          <div class="goods-list">
            <ul class="yui3-g">
              <!--商品-->
              <li
                class="yui3-u-1-5"
                v-for="good in searchData.goodsList"
                :key="good.id"
              >
                <div class="list-wrap">
                  <div class="p-img">
                    <a href="javascript:;" @click="toDetail(good.id)">
                      <img :src="good.defaultImg" />
                    </a>
                  </div>
                  <div class="price">
                    <strong>
                      <em>¥ </em>
                      <i>{{ good.price }}</i>
                    </strong>
                  </div>
                  <div class="attr">
                    <a target="_blank" href="item.html" :title="good.title">
                      {{ good.title }}
                    </a>
                  </div>
                  <div class="commit">
                    <i class="command">已有<span> 2000 </span>人评价</i>
                  </div>
                  <div class="operate">
                    <a
                      href="success-cart.html"
                      target="_blank"
                      class="sui-btn btn-bordered btn-danger"
                      >加入购物车</a
                    >
                    <a href="javascript:void(0);" class="sui-btn btn-bordered"
                      >收藏</a
                    >
                  </div>
                </div>
              </li>
            </ul>
          </div>
          <!--分页组件-->
          <el-pagination
            :total="searchData.total"
            :current-page="options.pageNo"
            :page-size="options.pageSize"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :page-sizes="[5, 10, 20, 30, 40]"
            layout="total, sizes, prev, pager, next, jumper"
          >
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import SearchSelector from "./SearchSelector/SearchSelector";
import { mapActions, mapState } from "vuex";
export default {
  name: "sph-search",
  props: [
    "keyword",
    "categoryName",
    "category1Id",
    "category2Id",
    "category3Id",
  ],
  data() {
    return {
      //当前搜索商品所需要的请求参数
      options: {
        /*分类&搜索关键字 跟路由密切结合的*/
        category1Id: "",
        category2Id: "",
        category3Id: "",
        categoryName: "",
        keyword: "",

        /*分页相关的请求字段*/
        pageNo: 1, //element分页组件的初始页
        pageSize: 5,

        /*品牌过滤字段*/
        trademark: "",

        /*商品属性数组 "113:U型领:领型"*/
        props: [],

        /*排序信息 */
        order: "2:asc",
      },
    };
  },
  computed: {
    ...mapState(["sortBtns"]),
    ...mapState({ searchData: (state) => state.search.searchData }),
    //全部结果中显示分类时到底显示第几级
    level() {
      if (this.category1Id) {
        return "一";
      } else if (this.category2Id) {
        return "二";
      } else {
        return "三";
      }
    },
  },
  methods: {
    ...mapActions(["getGoods"]),
    //分页中的当前页产生了改变
    async handleSizeChange(pageSize) {
      //pageSize 改变时会触发
      //当pageSize数据发生改变时 发送请求获取对应页的商品数据
      this.options.pageSize = pageSize;
      //pageSize 改变时 必须回到第一页!
      this.options.pageNo = 1;
      await this.getGoods(this.options);
    },
    async handleCurrentChange(currentPage) {
      //currentPage 改变时会触发(点击分页按钮时 产生currentPage时 才会触发当前回调)
      //当分页数据发生改变时 发送请求获取对应页的商品数据
      this.options.pageNo = currentPage;
      await this.getGoods(this.options);
    },
    //根据搜索商品条件的变化 来 同步请求参数
    updateOptions(newRoute) {
      //newRoute: 最新的路由对象
      this.options = {
        ...this.options,
        category1Id: newRoute.query.category1Id,
        category2Id: newRoute.query.category2Id,
        category3Id: newRoute.query.category3Id,
        categoryName: newRoute.query.categoryName,
        keyword: newRoute.params.keyword,
      };
    },
    //删除关键字
    removeKeyWord() {
      this.options.keyword = "";
      //同步URL: push; 保留query hash 剔除 parmas
      //watch.$route : 重新发请求&分页要回到第一页
      this.$router.push({
        name: "search",
        query: this.$route.query,
        hash: `#${Date.now()}`,
      });
      //清除输入框中的关键字
      this.$bus.$emit("clearKeyWord");
    },
    //删除分类信息
    removeCategory() {
      //同步URL: push; 保留parmas hash 剔除 query
      //watch.$route : 重新发请求&分页要回到第一页
      this.options.category1Id = "";
      this.options.category2Id = "";
      this.options.category3Id = "";
      this.options.categoryName = "";
      this.$router.push({
        name: "search",
        parmas: this.$route.parmas,
        hash: `#${Date.now()}`,
      });
    },
    //删除品牌信息
    async removeTrademark() {
      this.options.trademark = "";
      //重新发请求获取过滤完的商品数据;默认回第一页
      this.options.pageNo = 1;
      await this.getGoods(this.options);
    },
    //删除销售属性
    async removeProp(index) {
      this.options.props.splice(index, 1);
      //重新发请求获取过滤完的商品数据;默认回第一页
      this.options.pageNo = 1;
      await this.getGoods(this.options);
    },
    //排序规则发生了改变
    async btnChangeFn(order) {
      this.options.order = order;
      // //重新发请求获取过滤完的商品数据;默认回第一页
      this.pageNo = 1;
      await this.getGoods(this.options);
    },
    //跳转到商品详情页
    toDetail(id) {
      console.log(id);
      this.$router.push(`/detail/${id}`);
    },
  },
  components: {
    "sph-searchSelector": SearchSelector,
  },
  watch: {
    $route: {
      async handler(newRoute) {
        //搜索商品的条件发生改变;整个分页要重置到第一页
        this.options.pageNo = 1;
        //newRoute : 最新的$route
        //当前组件初始化时 当前回调也会调用 ; 初始化时不用带任何的搜索条件 ; 请求参数为空{}
        //搜索商品的条件发生改变时 请求参数也要变化!!!
        this.updateOptions(newRoute);
        //搜索商品的条件发生改变时触发的函数 在这个函数内要发送请求获取第一页的商品数据
        await this.getGoods(this.options);
      },
      deep: true,
      immediate: true,
    },
  },
  mounted() {
    //接收来自SearchSelector组件的trademark
    this.$bus.$on("sendTrademark", async (trademark) => {
      //重复点击相同的品牌 我们不做任何响应
      if (trademark === this.options.trademark) {
        return;
      }
      //获取商品列表的请求数据得到改变!
      this.options.trademark = trademark;
      //重新发请求获取过滤完的商品数据;默认回第一页
      this.options.pageNo = 1;
      await this.getGoods(this.options);
    });

    //接收来自SearchSelector组件的attr
    this.$bus.$on("sendAttr", async (attr) => {
      //重复点击相同的销售属性 我们不做任何响应
      if (this.options.props.includes(attr)) {
        return;
      }
      //获取商品列表的请求数据得到改变!
      this.options.props.push(attr);
      //重新发请求获取过滤完的商品数据;默认回第一页
      this.options.pageNo = 1;
      await this.getGoods(this.options);
    });
  },
};
</script>

<style scoped lang="less">
.main {
  margin: 10px 0;

  .py-container {
    width: 1200px;
    margin: 0 auto;

    .bread {
      margin-bottom: 5px;
      overflow: hidden;

      .sui-breadcrumb {
        padding: 3px 15px;
        margin: 0;
        font-weight: 400;
        border-radius: 3px;
        float: left;

        li {
          display: inline-block;
          line-height: 18px;

          a {
            color: #666;
            text-decoration: none;

            &:hover {
              color: #4cb9fc;
            }
          }
        }
      }

      .sui-tag {
        margin-top: -5px;
        list-style: none;
        font-size: 0;
        line-height: 0;
        padding: 5px 0 0;
        margin-bottom: 18px;
        float: left;

        .with-x {
          font-size: 12px;
          margin: 0 5px 5px 0;
          display: inline-block;
          overflow: hidden;
          color: #000;
          background: #f7f7f7;
          //padding: 0 7px;
          height: 20px;
          line-height: 20px;
          border: 1px solid #dedede;
          white-space: nowrap;
          transition: color 400ms;
          cursor: pointer;

          i {
            margin-left: 4px;
            cursor: pointer;
            font: 400 14px tahoma;
            display: inline-block;
            height: 100%;
            //vertical-align: middle;
          }

          &:hover {
            color: #28a3ef;
          }
        }
      }
    }

    .details {
      margin-bottom: 5px;

      .sui-navbar {
        overflow: visible;
        margin-bottom: 0;
      }

      .goods-list {
        margin: 20px 0;

        ul {
          display: flex;
          flex-wrap: wrap;

          li {
            height: 100%;
            width: 20%;
            margin-top: 10px;
            line-height: 28px;

            .list-wrap {
              .p-img {
                padding-left: 15px;
                width: 215px;
                height: 255px;

                a {
                  color: #666;

                  img {
                    max-width: 100%;
                    height: auto;
                    vertical-align: middle;
                  }
                }
              }

              .price {
                padding-left: 15px;
                font-size: 18px;
                color: #c81623;

                strong {
                  font-weight: 700;

                  i {
                    margin-left: -5px;
                  }
                }
              }

              .attr {
                padding-left: 15px;
                width: 85%;
                overflow: hidden;
                margin-bottom: 8px;
                min-height: 38px;
                cursor: pointer;
                line-height: 1.8;
                display: -webkit-box;
                -webkit-box-orient: vertical;
                -webkit-line-clamp: 2;

                a {
                  color: #333;
                  text-decoration: none;
                }
              }

              .commit {
                padding-left: 15px;
                height: 22px;
                font-size: 13px;
                color: #a7a7a7;

                span {
                  font-weight: 700;
                  color: #646fb0;
                }
              }

              .operate {
                padding: 12px 15px;

                .sui-btn {
                  display: inline-block;
                  padding: 2px 14px;
                  box-sizing: border-box;
                  margin-bottom: 0;
                  font-size: 12px;
                  line-height: 18px;
                  text-align: center;
                  vertical-align: middle;
                  cursor: pointer;
                  border-radius: 0;
                  background-color: transparent;
                  margin-right: 15px;
                }

                .btn-bordered {
                  min-width: 85px;
                  background-color: transparent;
                  border: 1px solid #8c8c8c;
                  color: #8c8c8c;

                  &:hover {
                    border: 1px solid #666;
                    color: #fff !important;
                    background-color: #666;
                    text-decoration: none;
                  }
                }

                .btn-danger {
                  border: 1px solid #e1251b;
                  color: #e1251b;

                  &:hover {
                    border: 1px solid #e1251b;
                    background-color: #e1251b;
                    color: white !important;
                    text-decoration: none;
                  }
                }
              }
            }
          }
        }
      }

      .page {
        width: 733px;
        height: 66px;
        overflow: hidden;
        float: right;

        .sui-pagination {
          margin: 18px 0;

          ul {
            margin-left: 0;
            margin-bottom: 0;
            vertical-align: middle;
            width: 490px;
            float: left;

            li {
              line-height: 18px;
              display: inline-block;

              a {
                position: relative;
                float: left;
                line-height: 18px;
                text-decoration: none;
                background-color: #fff;
                border: 1px solid #e0e9ee;
                margin-left: -1px;
                font-size: 14px;
                padding: 9px 18px;
                color: #333;
              }

              &.active {
                a {
                  background-color: #fff;
                  color: #e1251b;
                  border-color: #fff;
                  cursor: default;
                }
              }

              &.prev {
                a {
                  background-color: #fafafa;
                }
              }

              &.disabled {
                a {
                  color: #999;
                  cursor: default;
                }
              }

              &.dotted {
                span {
                  margin-left: -1px;
                  position: relative;
                  float: left;
                  line-height: 18px;
                  text-decoration: none;
                  background-color: #fff;
                  font-size: 14px;
                  border: 0;
                  padding: 9px 18px;
                  color: #333;
                }
              }

              &.next {
                a {
                  background-color: #fafafa;
                }
              }
            }
          }

          div {
            color: #333;
            font-size: 14px;
            float: right;
            width: 241px;
          }
        }
      }
    }
  }
}
</style>